<?php
namespace Admin\Controller;

class GoodsController extends AdminController
{
    public function goods()
    {
        $nickname      = I('nickname');
        $map['status'] = array('egt', 0);
        if (is_numeric($nickname)) {
            $map['id|title'] = array(intval($nickname), array('like', '%' . $nickname . '%'), '_multi' => true);
        } else {
            $map['title'] = array('like', '%' . (string) $nickname . '%');
        }

        $list = $this->lists('goods', $map);
        $this->assign('_list', $list);
        $this->meta_title = '商品管理';
        $this->display();
    }

    public function update()
    {
        if (IS_POST) {
            $id    = session('id');
            $goods = D('goods')->create(I('post.'));
            if ($goods) {
                if (D('goods')->where(array('id' => $id))->save()) {
                    session('id', null);
                    $this->success('修改成功', U('goods'));
                } else {
                    $this->error(D('goods')->getError());
                }
            } else {
                $this->error($D('goods')->getError());
            }
        } else {
            $id       = I('get.id');
            $res      = M('goods')->where(array('id' => $id))->select()[0];
            $classify = M('classify')->where(array('fid' => 0))->select();
            if ($res) {
                session('id', $id);
                $this->assign('res', $res);
                $this->assign('classify', $classify);
            } else {
                $this->error(M('goods')->getError());
            }
            $this->display();
        }
    }

    public function delete()
    {
        $id  = I('get.id');
        $res = M('goods')->where(array('id' => $id))->delete();
        if ($res) {
            $this->success('已成功删除此商品！', U('goods'));
        } else {
            $this->error(M('goods')->getError());
        }
    }

    public function add()
    {
        if (IS_POST) {
            $goods = D('goods');
            $res1  = $goods->create(I('post.'));
            if ($res1) {
                if ($goods->add()) {
                    $this->success('添加成功', U('goods'));
                } else {
                    $this->error($goods->getError());
                }
            } else {
                $this->error($goods->getError());
            }
        } else {
            $classify = M('classify')->where(array('fid' => 0))->select();
            $this->assign('classify', $classify);
            $this->display();
        }
    }

    public function upload()
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->savePath = '/Public/Uploads/'; // 设置附件上传目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            // 上传成功
            return $info;
        }
    }

    public function ajaxSelect()
    {
        if (I('get.id')) {
            $id   = I('get.id');
            $res1 = M('classify')->where(array('fid' => $id))->select();
        }else{
        	return;
        }
        if ($res1) {
            $this->assign('res1', $res1);
            $html             = $this->fetch('ajaxSelect');
            $return['status'] = 1;
            $return['info']   = '成功';
            $return['data']   = $html;
        } else {
            $return['status'] = 0;
            $return['info']   = '失败';
            $return['data']   = null;
        }
        $this->ajaxReturn($return);
    }
}
